package me.zixing.java.guiandthread.domain;

import lombok.*;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created by bingh on 2017/11/13.
 * private volatile double x;//一个线程写，可见性同步
 * private volatile double y;//一个线程写，可见性同步
 * private long elapsedTime;//线程封闭
 * private volatile int STATE;//0 (未开始), 1 (进行中), 2 (已结束) ;线程不安全
 */

public class SportableContent implements Runnable{

    @Getter
    @Setter
    private String name;
    @Getter
    protected volatile double x;
    @Getter
    protected volatile double y;
    @Getter
    protected long elapsedTime;
    @Getter
    @Setter
    protected volatile int STATE;
    private int length;//跑多少m

    //监听器
    private List<SportableContentListener> listeners = new ArrayList<>();

    public SportableContent(String name,double x, double y, int length, int STATE) {
        this.name = name;
        this.x = x;
        this.y = y;
        this.length = length;
        this.STATE = STATE;
    }

    protected SportableContent(){}

    @Override
    public void run() {
        try{
            System.out.println(name+"等待...");
            Track.getLatch().await();
            System.out.println(name+"开始...");
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void addListener(SportableContentListener listener){
        listeners.add(listener);
    }

    public void fireListener(SportableContentEvent eventObject){
        for (SportableContentListener listener:listeners) {
            listener.doWithEvent(eventObject);
        }
    }

    @Override
    public String toString(){
        String s = getClass().getSimpleName()+"("+
                "x:"+this.x+"\t"+
                "y:"+this.y+"\t"+
                "STATUS:"+this.STATE+"\t"+
                "elapsedTime:"+this.elapsedTime +
                ")";
        return s;
    }
}
